package cn.yitun.chat.yitunserverimcore.general.main.push.chat;

import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.Motion;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserChatContentDto;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.user.UserChatPushMethodEnum;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户消息推送
 * @author: yitun
 * @date: 2016年8月26日 上午9:21:41
 */
@Service
public class UserChatPush {
	//1.3.100
	final String action = ActionTypeConstant.USER_CHAT_ACTION_PUSH;

	@Resource
	WriteHandler writeHandler;

	/**
	 * 特定方式推送
	 * @param key
	 * @param sendUserId
	 * @param query
	 */
	public void motion(Long key, Long sendUserId, UserChatContentDto query) {
		Long receiveUserId = query.getReceiveUserId();
		Motion motion = query.getMotion();
		List<Long> idList = new ArrayList<>();
		idList.add(receiveUserId);
		idList.add(sendUserId);

		PushMessage message = new PushMessage();
		message.bodyPut("sendUserId", sendUserId);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("messageKey", key);
		message.bodyPut("motion", motion);

		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(UserChatPushMethodEnum.USER_CHAT_MOTION.getMethod());
		head.setKey(key);
		head.setTimestamp(System.currentTimeMillis());
		message.setHead(head);
		writeHandler.push(YitunStringUtil.toStrList(idList), message);
	}

	/**
	 * Description 推送私聊信息 <br>
	 * Date 2019-05-22 20:26:49<br>
	 * 
	 * @param key
	 * @param sendUserId
	 * @param receiveUserId
	 * @param content       void
	 * @version 1.1
	 */
	public void pushUserChat(Long key, Long sendUserId, Long receiveUserId, Content content) {

		List<Long> idList = new ArrayList<>();
		idList.add(receiveUserId);
		idList.add(sendUserId);

		PushMessage message = new PushMessage();
		message.bodyPut("sendUserId", sendUserId);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("content", content);

		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(UserChatPushMethodEnum.USER_CHAT_PUSH.getMethod());
		head.setKey(key);
		head.setTimestamp(System.currentTimeMillis());
		message.setHead(head);
		writeHandler.push(YitunStringUtil.toStrList(idList), message);
	}
}
